//
//  NOstuDetailVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/4/14.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "NOstuDetailVC.h"

#import "Masonry.h"

#import "Controller.h"
#import "MyOrderData.h"
#import "MyorderDataList.h"
#import "TongYong.h"

#import "orderCell.h"

#import "DetaileButandLabelCell.h"

#import "orderHeaderView.h"

#import "daizhifuVC.h"

#import "MyorderVC.h"

#import "huopinVC.h"

#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height
#define bgVCcolor [UIColor colorWithRed:235.f/255.f green:235.f/255.f blue:244.f/255.f alpha:1]

@interface NOstuDetailVC ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate> {
    UIView *topview;//顶部两个列表
    NSMutableArray *arrayList;
    NSString *dingdanhaost;
    NSString *statusst;
    NSString *compayst;
    NSString *numst;
}

@property(nonatomic,strong) UITableView *tableview;

@end

@implementation NOstuDetailVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"NOstuDetailVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"NOstuDetailVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden=YES;
    
    self.view.backgroundColor=bgVCcolor;
    //加一个导航条
    UIView*navcBar=[[UIView alloc]init];
    navcBar.backgroundColor=[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1];
    [self.view addSubview:navcBar];
    [self.view bringSubviewToFront:navcBar];
    [navcBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);   make.left.offset(0);
        make.height.offset(kNavigationBarHeight);  make.right.offset(0);
    }];
    //导航条标题
    UILabel*titleLabel=[[UILabel alloc]init];
    titleLabel.text=@"订单详情";
    titleLabel.font=Font(17);
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor=[UIColor whiteColor];
    [navcBar addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25);
        make.left.offset(self.view.frame.size.width/2-70);
        make.width.offset(140);
        make.height.offset(30);
    }];
    
    //返回按钮
    UIButton*leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(backLogin) forControlEvents:UIControlEventTouchUpInside];
    [navcBar addSubview:leftButton];
    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(6);
        make.width.offset(24);
        make.height.offset(22);
        make.top.offset(31);
    }];
    
    
    
    //订单号   公司名称
    topview =[[UIView alloc]initWithFrame:CGRectMake(0, 70, screenWidth, 84)];
    topview.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:topview];
    
    //订单名称和订单号
    UILabel  *companyorderlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 11, 165, 20)];
    companyorderlabel.textAlignment=NSTextAlignmentLeft;
    companyorderlabel.font=[UIFont systemFontOfSize:12];
    companyorderlabel.tag=9000;
    [topview addSubview:companyorderlabel];
    
    //状态
    UILabel  *statusla=[[UILabel alloc]initWithFrame:CGRectMake(screenWidth-100-10, 11, 100, 20)];
    statusla.textAlignment=NSTextAlignmentRight;
    statusla.font=[UIFont systemFontOfSize:12];
    statusla.textColor=[UIColor colorWithRed:251.f/255.f green:87.f/255.f blue:60.f/255.f alpha:1];
    statusla.tag=9001;
    [topview addSubview:statusla];
    
    UIImageView *line=[[UIImageView alloc]initWithFrame:CGRectMake(0, 42, self.view.frame.size.width, 1)];
    line.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    [topview addSubview:line];
    
    //公司名称
    
    UILabel  *comlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 53, 165, 20)];
    comlabel.textAlignment=NSTextAlignmentLeft;
    comlabel.font=[UIFont systemFontOfSize:12];
    comlabel.textColor=[UIColor colorWithRed:57.f/255.f green:93.f/255.f blue:124.f/255.f alpha:1];
    comlabel.tag=9002;
    [topview addSubview:comlabel];
    
    UILabel  *numla=[[UILabel alloc]initWithFrame:CGRectMake(screenWidth-100-10-20, 53, 100, 20)];
    numla.textAlignment=NSTextAlignmentRight;
    numla.font=[UIFont systemFontOfSize:12];
    numla.textColor=[UIColor colorWithRed:135.f/255.f green:135.f/255.f blue:135.f/255.f alpha:1];
    numla.tag=9003;
    [topview addSubview:numla];
    
    UIImageView *jiantouImageV=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-10-10, 55.5, 10, 15)];
    jiantouImageV.image=[UIImage imageNamed:@"ic_arraw"];
    [topview addSubview:jiantouImageV];
    //大的按钮点击进订单界面
    UIButton *orderBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    orderBtn.frame=CGRectMake(0, 43, self.view.frame.size.width, 40);
    orderBtn.backgroundColor=[UIColor clearColor];
    [orderBtn addTarget:self action:@selector(orderBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [topview addSubview:orderBtn];

    
    //下面打电话试图
    //下部按钮
    UIView *bootmView=[[UIView alloc]init];
    bootmView.backgroundColor=[UIColor clearColor];
    bootmView.frame=CGRectMake(0,screenHeight-50, screenWidth, 50);
    [self.view addSubview:bootmView];
    
    UILabel *leftlabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, screenWidth-20, 30)];
    leftlabel.font=[UIFont systemFontOfSize:14];
    leftlabel.textAlignment=NSTextAlignmentCenter;
    leftlabel.textColor=[UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0  alpha:1];
    [bootmView addSubview:leftlabel];
    NSString *ss=[NSString stringWithFormat:@"如有问题请拨打客服热线:  400-885-0054"];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:ss];
    NSRange redRange = NSMakeRange([[noteStr string] rangeOfString:@":"].location+1,noteStr.length-12);
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:90.f/255.f green:191.f/255.f blue:219.f/255.f alpha:1] range:redRange];
    [leftlabel setAttributedText:noteStr];
    
    UIButton *iphonebut=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2, 0, screenWidth/2, 30)];
    iphonebut.backgroundColor=[UIColor clearColor];
    [iphonebut addTarget:self action:@selector(iphoneButAction) forControlEvents:UIControlEventTouchUpInside];
    [bootmView addSubview:iphonebut];
    
    
    
    //定义数组
    arrayList=[[NSMutableArray alloc]init];
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0,160, self.view.frame.size.width, self.view.frame.size.height-160-50) style:UITableViewStyleGrouped];
    _tableview.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.separatorStyle=NO;
    
    [self.view addSubview:_tableview];
    
    //请求数据
    [self getdetailData];

    
    
    
    

}

//进入货品清单
-(void)orderBtnAction
{
    //取出对应段落的数据
    MyOrderData *order3=[[MyOrderData alloc]initWith:arrayList[0]];
    
    NSMutableArray *OrderArray=[[NSMutableArray alloc]init];
    OrderArray= order3.pro_data;
    //    货品清单
    huopinVC *hpVc=[[huopinVC alloc]init];
    hpVc.OrderArray=OrderArray;
    hpVc.isXianhuo=@"订单详情";
    hpVc.zongmoney=order3.total_sales;
    [self.navigationController pushViewController:hpVc animated:YES];
    
}



-(void)getdetailData
{
    //获取 需要支付的金额 以及 账户余额
    NSMutableDictionary *di=[[NSMutableDictionary alloc]init];
    [di setValue:@"" forKey:@"member_id"];
    [di setValue:self.intent_order_id forKey:@"intent_order_id"];
    //要判断什么类型的然后穿的参数不同
    [Controller requestWithURL:ORDERDETAIL jsondic:di finishBlock:^(ClassData* data) {
        
        if ([data.number isEqualToNumber:[NSNumber numberWithInteger:0]])
        {
            [arrayList addObjectsFromArray:data.data[@"data"]];
            
            [_tableview reloadData];
            
            dingdanhaost=[NSString stringWithFormat:@"订单号:%@",arrayList[0][@"intent_order_code"]];
            
            NSString *st=arrayList[0][@"status"];
            NSInteger status=st.integerValue;
            if (status==10) {
                statusst=@"待支付";
            }else if(status==15){
                statusst=@"已付定金";
            }else if(status==20){
                statusst=@"已结算";
            }else if(status==-1){
                statusst=@"已失效";
            }else if(status==40){
                statusst=@"已退款";
            }
            
            
            compayst=arrayList[0][@"invoice_company"];
//            numst=[NSString stringWithFormat:@"共%@件",arrayList[0][@"total_quantity"]];
            NSMutableArray *OrderArray=[[NSMutableArray alloc]init];
            OrderArray= arrayList[0][@"pro_data"];
            numst=[NSString stringWithFormat:@"共%lu件",(unsigned long)OrderArray.count];
            
            [self loadmeagesView];
            
        }else
        {
            ALERTSHOW(data.message);
            
            
        }
    }];
    
    
    
    
}

-(void)loadmeagesView
{
    UILabel *dingdan=[topview viewWithTag:9000];
    dingdan.text=dingdanhaost;
    
    UILabel *statusla=[topview viewWithTag:9001];
    statusla.text=statusst;
    
    UILabel *compay=[topview viewWithTag:9002];
    compay.text=compayst;
    
    UILabel *numla=[topview viewWithTag:9003];
    numla.text=numst;
    
}



#pragma  mark--------------表格代理方法-----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return arrayList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    MyOrderData *order=[[MyOrderData alloc]initWith:arrayList[section]];
    
    return order.pro_data.count;
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //指定cellIdentifier为自定义的cell
    static NSString *CellIdentifier = @"DetaileButandLabelCell";
    
    //取出对应段落的数据
    MyOrderData *order3=[[MyOrderData alloc]initWith:arrayList[indexPath.section]];
    
    MyorderDataList *orderListData=[[MyorderDataList alloc]initWith:order3.pro_data[indexPath.row]];
    
    //自定义cell类
    DetaileButandLabelCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    //无色
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (cell == nil) {
        //通过xib的名称加载自定义的cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DetaileButandLabelCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    //    4.1.1订单列表：展示货品参数
    //    展示购买的货品：
    //    型材 、管材 ： 品类（如扁钢等）、 材质、 规格、形状（如方管、圆管等，客户没有上传就不展示）、重量（总重）、数量、所在地、产地；
    //    卷、板、带：材质、表面、 规格(厚度*宽度*长度） 参厚 等级 重量（总重） 仓库、产地；
    
    
    //顶部字符串拼接
    NSString *toplable;
    if ([TongYong isBlankObjectAndStr:orderListData.suface_code]) {
        toplable=[NSString stringWithFormat:@"%@ %@",orderListData.category_name,orderListData.material_code];
    }else{
        toplable=[NSString stringWithFormat:@"%@ %@/%@",orderListData.category_name,orderListData.material_code,orderListData.suface_code];
        
    }
    
    
    
    NSString *guige=orderListData.spec_code;
    
    //参厚
    NSString *canhou;
    if ([TongYong isBlankObjectAndStr:orderListData.reference_thick ]) {
        canhou=@"";
    }else{
        canhou=[NSString stringWithFormat:@"(%@)",orderListData.reference_thick];
    }
    
    //边部
    NSString *bianbu;
    if ([TongYong isBlankObjectAndStr:orderListData.edge_name ]) {
        bianbu=@"";
    }else{
        
        bianbu=[NSString stringWithFormat:@"/%@",orderListData.edge_name];
        
    }
    
    //等级
    NSString *dengji;
    if ([TongYong isBlankObjectAndStr:orderListData.level_name ]) {
        
        dengji=@"";
        
    }else{
        
        dengji=[NSString stringWithFormat:@"/%@",orderListData.level_name];
        
    }
    
    
    //包装
    NSString *baozhuang;
    if ([TongYong isBlankObjectAndStr:orderListData.package_type ]) {
        
        baozhuang=@"";
        
    }else{
        //包装类型//(30,"裸包"),(10,"简包"),(20,"精包"),(0,"正常包装")
        NSInteger typ=orderListData.package_type.integerValue;
        if (typ==30) {
            baozhuang=[NSString stringWithFormat:@"/裸包"];
        }else if(typ==20){
            baozhuang=[NSString stringWithFormat:@"/精包"];
        }else if(typ==10){
            baozhuang=[NSString stringWithFormat:@"/简包"];
        }else{
            
            baozhuang=[NSString stringWithFormat:@"正常包装"];
        }
    }
    
    
    //形状
    NSString *xinzhuang;
    if ([TongYong isBlankObjectAndStr:orderListData.shape_name ]) {
        
        xinzhuang=@"";
        
    }else{
        
        xinzhuang=[NSString stringWithFormat:@"/%@",orderListData.shape_name];
        
    }
    
    //品名
    NSString *pingming;
    if ([TongYong isBlankObjectAndStr:orderListData.category_name ]) {
        
        pingming=@"";
        
    }else{
        
        pingming=[NSString stringWithFormat:@"/%@",orderListData.category_name];
        
    }
    
    
    
    NSString *bigTEXT;
    if ([orderListData.category_name isEqualToString:@"板"]||[orderListData.category_name isEqualToString:@"卷"]||[orderListData.category_name isEqualToString:@"板材"]||[orderListData.category_name isEqualToString:@"卷材"]) {
        //		板卷   规格(参厚)/边部/等级/包装
        
        bigTEXT=[NSString stringWithFormat:@"%@  %@%@%@%@%@",toplable,guige,canhou,bianbu,dengji,baozhuang];
        
        
    }else{
        
        bigTEXT=[NSString stringWithFormat:@"%@  %@%@%@%@%@%@",toplable,guige,pingming,xinzhuang,bianbu,dengji,baozhuang];
    }
    
    
    
    
    //仓库  生产商
    NSString *ck;
    if ([TongYong isBlankObjectAndStr:orderListData.house_name ]) {
        
        ck=@"";
        
    }else{
        
        ck=[NSString stringWithFormat:@"%@",orderListData.house_name];
        
    }
    
    NSString *scs;
    if ([TongYong isBlankObjectAndStr:orderListData.manufacturer_name ]) {
        
        scs=@"";
        
    }else{
        
        scs=[NSString stringWithFormat:@"%@",orderListData.manufacturer_name];
        
    }
    
    
    NSString *stbiglabel=[NSString stringWithFormat:@"%@  %@",bigTEXT,ck];
    
    cell.biglabelname.text=stbiglabel;
    
    //算总重
    float quight=orderListData.quantity.floatValue;
    float weight=orderListData.weight.floatValue;
    float zongzhong=weight*quight;
    
    cell.weightLabel.text=[NSString stringWithFormat:@"%.3f 吨",zongzhong/1000];
    
    //    cell.addandhous.text=[NSString stringWithFormat:@"%@    %@",ck,scs];
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//区头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
//返回区头View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    orderHeaderView *headView=[[orderHeaderView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 40)];
    
    headView.contentView.backgroundColor=[UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
    
    headView.jiantouImageV.hidden=YES;
    headView.companyorderlabel.text=@"货品清单:";
    
    return headView;
}

-(void)backLogin
{
    if ([self.isdaizhifu isEqualToString:@"待支付列表"]) {
        
        
        NSArray *viewControlellers = self.navigationController.viewControllers;
        
        daizhifuVC *existViewController;
        
        
        for (UIViewController *viewController in viewControlellers) {
            if ([viewController isKindOfClass:[daizhifuVC class]]) {
                
                existViewController = (daizhifuVC *)viewController;
                
                
            }
        }
        
        [self.navigationController popToViewController:existViewController animated:YES];

        
    }else if ([self.isdaizhifu isEqualToString:@"全部订单列表"]) {
        
        
        NSArray *viewControlellers = self.navigationController.viewControllers;
        
        MyorderVC *existViewController;
        
        
        for (UIViewController *viewController in viewControlellers) {
            if ([viewController isKindOfClass:[MyorderVC class]]) {
                
                existViewController = (MyorderVC *)viewController;
                
                
            }
        }
        
        [self.navigationController popToViewController:existViewController animated:YES];

        
    }else{
    
       [self.navigationController popViewControllerAnimated:YES];
    }
    

    
}

//拨打电话
- (void)iphoneButAction {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"友情提醒" message:@"客服工作时间:周一到周五\n(8:30~17:30)" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"呼叫客服", nil];
    alertView.delegate = self;
    [alertView show];
}

#pragma mark 呼叫客服
- (void)callCustomerService {
    UIWebView *webview = [[UIWebView alloc] init];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"tel:4008850054"]]];
    [self.view addSubview:webview];
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (0 == buttonIndex) {
        // do nothing
    } else {
        [self callCustomerService];
    }
}

@end
